package com.homwee.factory.tvtool;

import com.homwee.factory.R;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AlertDialog {
	Context mContext;

	WindowManager mWindowManager;
	WindowManager.LayoutParams mLayoutParams;
	LayoutInflater mLayoutInflater;

	LinearLayout mLayout;

	TextView mTvMessage;
	Button mBtSave, mBtUnsave, mBtCancel;

	public AlertDialog(Context cxt, View.OnClickListener saveListener,
			View.OnClickListener unSaveListener) {
		super();
		mContext = cxt;

		mWindowManager = (WindowManager) mContext
				.getSystemService(mContext.WINDOW_SERVICE);
		mLayoutInflater = LayoutInflater.from(mContext);
		mLayoutParams = new WindowManager.LayoutParams();
		mLayoutParams.type = LayoutParams.TYPE_PHONE;
		mLayoutParams.format = PixelFormat.RGBA_8888;
		mLayoutParams.gravity = Gravity.CENTER;
		/*
		 * mLayoutParams.x = 0; mLayoutParams.y = 0;
		 */
		mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

		mLayout = (LinearLayout) mLayoutInflater.inflate(R.layout.alert_dialog,
				null);
		mTvMessage = (TextView) mLayout.findViewById(R.id.tv_message);
		mBtSave = (Button) mLayout.findViewById(R.id.bt_save);
		mBtUnsave = (Button) mLayout.findViewById(R.id.bt_unsave);
		mBtCancel = (Button) mLayout.findViewById(R.id.bt_cancel);

		mBtSave.setOnClickListener(saveListener);

		mBtUnsave.setOnClickListener(unSaveListener);

		mBtCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				StaticValues.bSaveAQAndExit = false;
				StaticValues.bSavePQAndExit = false;
				hide();
			}
		});

	}
	
	public void setMessage(String strMsg)
	{
		mTvMessage.setText(strMsg);
	}
	
	public void setSaveButtonAndUnSaveButtonInvisible()
	{
		mBtSave.setVisibility(View.GONE);
		mBtUnsave.setVisibility(View.GONE);
	}
	
	public void setTextSize(int textSize)
	{
		mTvMessage.setTextSize(textSize);
	}
	
	public void show() {
		mWindowManager.addView(mLayout, mLayoutParams);
		mBtSave.requestFocus();
	}

	public void hide() {
		mWindowManager.removeView(mLayout);
	}

}
